package com.szh.cloud.file.service;

import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;
import com.szh.cloud.file.repository.FileDao;
import com.szh.cloud.file.service.impl.FileServiceImpl;
import com.szh.cloud.mybatis.entity.PtFileEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service
public class FileSynServiceImpl implements FileSynService {
    @Resource
    private FileService fileService;

    private static Log logger= LogFactory.getLog(FileServiceImpl.class);

    @Value("${end.point}")
    private String endpoint = "";
    @Value("${ak}")
    private String ak = "";
    @Value("${sk}")
    private String sk = "";
    @Value("${bucket.name}")
    private String bucketName = "";
    @Resource
    private FileDao fileDao;
    @Override
    public void copyPasteFile(String fileUuidArray, String descDir) {
        String[] fileUuids = fileUuidArray.split(",");
        List<PtFileEntity> ptFileEntityList = fileDao.queryFileListByArray(fileUuids);
        String descPath = "";
        ObsClient obsClient = new ObsClient(ak,sk,endpoint);
        ObsObject obsObject;
        InputStream inputStream;
        for(PtFileEntity item:ptFileEntityList){
            descDir = item.getFileName();
            if(FileService.File_SEPARATOR.equals(descDir)){
                descPath = item.getFileName();
            }else{
                descPath = descDir + item.getFileName();
            }
            obsObject = obsClient.getObject(bucketName,item.getFilePath());
            inputStream = obsObject.getObjectContent();
            obsClient.putObject(bucketName,descPath,inputStream);

        }
        fileService.synchFileData();


    }
}
